// INCLUDES --------------------------------------------------------------------
#include "Image.h"

// DEFINES ---------------------------------------------------------------------

// TYPEDEFS --------------------------------------------------------------------

// IMAGE FILE HEADERS ----------------------------------------------------------
#define PPMv6 "P6\n\n%i %i\n255\n" // PPM v6 -> supports: RGB8

// FUNCTIONS -------------------------------------------------------------------

// write framebuffer to disk as PPM
bool ImageWritePPM(i8 *filename, FramebufferRGB8 *raster)
{
  GUARD_NULL("ImageWritePPM *filename", filename, false);
  GUARD_NULL("ImageWritePPM *raster", raster, false);

  // check for a valid raster to process
  if (FramebufferCheck(raster) == false) return false; // exit in error!

  // pointer to our new PPM image
  FILE *file = NULL;
/*
  // create a proper filename
  STRING_GEN("%s.ppm", filename); 
  if (stringGenSuccess == -1) goto fail;
*/
  // open up a new image file in "overwrite" mode
  file = fopen(filename, "wb"); //file = fopen(stringGen, "wb");
  GUARD_NULL("ImageWritePPM *file", file, false);

  // assemble file header
  //STRING_GEN(PPMv6, raster->width, raster->height);
  //if (stringGenSuccess == -1) goto fail;

  // write the file header
  i32 ret = fprintf(file, "%s", stringGen);
  if (ret < 0) goto fail;

  // write pixel data to disk
  /*i32 pixelAmount = raster->width * raster->height;              // calculate amount of pixels
  fwrite(raster->buffer, sizeof(ColorRGB8), pixelAmount, file);  // write pixels from the framebuffer
*/
  // close file
  fclose(file);

  return true;

fail:
  fclose(file); // close file
  return false; // exit in error
}; 
